<?php
/**
 * UserCoupon.class.php
 *
 * @author camfee <camfee@foxmail.com>
 * @date   18-1-15 下午1:17
 */

namespace Center;

use Common\ModuleBase;
use Common\Bridge;

class UserShop extends ModuleBase
{
    protected static $_conf = [
        'db' => [
            'w' => Bridge::DB_CENTER_W,
            'r' => Bridge::DB_CENTER_R
        ],
        'table' => 'UserShop',
        'fields' => [
            'Id' => self::VAR_TYPE_KEY,
            'UserId' => self::VAR_TYPE_INT,
            'ShopId' => self::VAR_TYPE_INT,
            'Type' => self::VAR_TYPE_INT,
            'Status' => self::VAR_TYPE_INT,
            'StartTime' => self::VAR_TYPE_STRING,
            'EndTime' => self::VAR_TYPE_STRING,
            'UpdateTime' => self::VAR_TYPE_STRING,
            'CreateTime' => self::VAR_TYPE_STRING,
        ],
        'mc' => Bridge::MEMCACHE_DEFAULT,
        // 可选, MC KEY, "KeyName:%d", %d会用主键ID替代
        'mckey' => 'UserShop:%d',
        // 可选, 超时时间, 默认不过期
        'mctime' => 86400,
    ];

    const STATUS_UN_USED = 1;
    const STATUS_USED = 2;
    const COUPON_VALID = 1;
    const COUPON_UN_VALID = 2;

    /**
     * 获取用户商家优惠券
     *
     * @param     $uid
     * @param int $status
     * @param int $valid
     * @param int $shopid
     * @param int $offset
     * @param int $limit
     * @return array
     */
    public static function getUserShopList($uid, $shopid, $status = 0, $valid = 0, $offset = 0, $limit = 100)
    {

        $where = [
            'UserId' => $uid,
            'ShopId' => $shopid
        ];
        if ($valid == self::COUPON_VALID) { // 结束时间或使用时间大于现在
            $where['EndTime >'] = date('Y-m-d H:i:s');
        } elseif ($valid == self::COUPON_UN_VALID) { //结束时间或使用时间小于现在
            $where['EndTime <='] = date('Y-m-d H:i:s');
        }
        if ($status > 0) {
            $where['Status'] = $status;
        }

        return self::getList($where, $offset, $limit, '*', 'UpdateTime DESC');
    }

    /**
     * @see ModuleBase::add() 新增
     * @see ModuleBase::update() 更新
     * @see ModuleBase::getInfoByIds() 按id查询
     * @see ModuleBase::getList() 条件查询
     * @see ModuleBase::delete() 删除
     */

}